package com.yoyoyo666.report.controller;

import com.github.pagehelper.StringUtil;
import com.yoyoyo666.report.common.AnnoLogin;
import com.yoyoyo666.report.common.DateUtils;
import com.yoyoyo666.report.common.IDUtils;
import com.yoyoyo666.report.mapper.ReportContentMapper;
import com.yoyoyo666.report.mapper.ReportMailMapper;
import com.yoyoyo666.report.model.LoginUser;
import com.yoyoyo666.report.model.ReportContent;
import com.yoyoyo666.report.model.ReportMail;
import com.yoyoyo666.report.model.common.BaseResult;
import com.yoyoyo666.report.model.common.ListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("mail")
public class MailController {


    @Autowired
    private ReportMailMapper reportMailMapper;

    @GetMapping("getAll")
    public ListResult<ReportMail> getContent(@AnnoLogin LoginUser user){
       return  ListResult.getSuccess( reportMailMapper.selectAllByUserId(user.getCurrentUser().getId()));
    }

    @PostMapping("add")
    public BaseResult add(@AnnoLogin LoginUser user ,ReportMail mail){
        mail.setUserId(user.getCurrentUser().getId());
        mail.setId(IDUtils.getId());
        mail.setCreateTime(DateUtils.getNowString());
        int insert = reportMailMapper.insert(mail);
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }
    }

    @PostMapping("modify")
    public BaseResult modify(@AnnoLogin LoginUser user ,ReportMail content){
        BaseResult fail = BaseResult.getFail();
        if(StringUtil.isEmpty(content.getId())){
            fail.setMsg("id不能为空");
            return fail;
        }

        int insert = reportMailMapper.updateByPrimaryKeySelective(content);
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }
    }

    @PostMapping("delete")
    public BaseResult delete(@AnnoLogin LoginUser user ,ReportMail content){
        BaseResult fail = BaseResult.getFail();
        if(StringUtil.isEmpty(content.getId())){
            fail.setMsg("id不能为空");
            return fail;
        }
        int insert = reportMailMapper.deleteByPrimaryKey(content.getId());
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }

    }
}
